package com.caltco.cargo.ops.modules.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.caltco.cargo.ops.auth.entity.UserEntity;
import com.caltco.cargo.ops.auth.service.UserService;
import com.caltco.cargo.ops.common.annotation.ApiSysLog;
import com.caltco.cargo.ops.common.api.R2;
import com.caltco.cargo.ops.common.api.Result;
import com.caltco.cargo.ops.common.exception.RRException;
import com.caltco.cargo.ops.modules.sys.entity.SysUserEntity;
import com.caltco.cargo.ops.modules.sys.request.LoginRequest;
import com.caltco.cargo.ops.modules.sys.request.RegisterRequest;
import com.caltco.cargo.ops.modules.sys.request.UpdateRequest;
import com.caltco.cargo.ops.modules.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;


/**
 * 登录接口
 *
 * @author
 * @email
 * @date 2017-03-23 15:31
 */
@Slf4j
@RestController("loginController")
@RequestMapping("/api")
@Tag(name = "用户认证")
public class ApiLoginController{
    @Autowired
    private SysUserService sysUserService;
    @Value("${agilefast.redis.open}")
    private String allowedOrigins;
    @Value("${agilefast.redis.open}")
    private String isOpenRedis;
    @Autowired
    private UserService userService;

    public UserEntity getCurUser(HttpServletRequest request) {
        UserEntity curUserInfo;
        if (isOpenRedis.equals("true")){
            curUserInfo = userService.getCurUserInfoByRedis(request);
        }else{
            curUserInfo = new UserEntity();
            curUserInfo.setUsername(request.getAttribute("userName").toString());
            curUserInfo.setUserId((Long)request.getAttribute("userId"));
        }
        return curUserInfo;
    }

    @PostMapping("logout")
    @Operation(summary = "退出")
    @ApiSysLog("退出")
    public Result<Boolean> logout(HttpServletRequest request) {
        UserEntity curUserInfo = getCurUser(request);
        return sysUserService.logout(curUserInfo.getUsername(), curUserInfo.getUserId()) ? R2.setOkMsg("退出登录成功"):R2.setErrMsg(("退出登录失败"));
    }

    @PostMapping("login")
    @Operation(summary = "Api登录授权")
    public Result<Map<String, Object>> loginv2(HttpServletRequest request, @RequestBody LoginRequest loginRequest, HttpSession session) {
        Map<String, Object> map = sysUserService.verifyLogin(loginRequest);
        return R2.ok(map);
    }

    @PostMapping("register")
    @Operation(summary = "注册")
    public Result<Boolean> register(HttpServletRequest request, @RequestBody RegisterRequest registerRequest, HttpSession session) {
        return R2.ok(sysUserService.saveUser(registerRequest));
    }

    @GetMapping("getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<SysUserEntity> getUserInfo(HttpServletRequest request) {
        SysUserEntity user = sysUserService.getUserInfo();
        return R2.ok(user);
    }


    @PutMapping("updateUser")
    @Operation(summary = "更新用户信息")
    public Result<SysUserEntity> register(HttpServletRequest request, @RequestBody UpdateRequest updateRequest) {
        SysUserEntity user = sysUserService.updateUser(updateRequest);
        return R2.ok(user);
    }

    @GetMapping("/common/verify")
    public void Verify(HttpServletRequest request, HttpServletResponse response) throws IOException {
         //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
        //图形验证码写出，可以写出到文件，也可以写出到流
        captcha.write(response.getOutputStream());
        //获取验证码中的文字内容
        String verifyCode = captcha.getCode();
        System.out.println("222" + verifyCode);
        request.getSession().setAttribute("verifyCode",verifyCode);
    }


    @PostMapping("/test")
    public void Test(@RequestBody LoginRequest request) {
        System.out.println(222);
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
//        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
//        //图形验证码写出，可以写出到文件，也可以写出到流
//        captcha.write(response.getOutputStream());
//        //获取验证码中的文字内容
//        String verifyCode = captcha.getCode();
//        System.out.println("222" + verifyCode);
//        request.getSession().setAttribute("verifyCode",verifyCode);
    }

}
